/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.LoanBillBean;
import com.minierp.bean.QueryParameterBean;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.LoanBillDao;
import com.minierp.dao.QueryDao;
import com.minierp.dao.TallySetDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncLoanBillMACtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.BankAccount;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.LoanBill;
import com.minierp.model.TallySet;
import com.minierp.service.LoanBillService;
import com.minierp.view.LoanBillListExcelView;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class LoanBillMACtr extends MultiActionController {
	private String dialogSuccessView;
	private String listForExamineView;
	private String infoForExamineView;
	private String listForUnExamineView;
	private String infoForUnExamineView;
	private String searchResultView;
	private String downloadView;
	private String listForCancelView;
	private String infoForCancelView;
	private String printView;
	private String countView;
	private LoanBillDao loanBillDao;
	private DepartmentDao departmentDao;
	private BankAccountDao bankAccountDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private BankAccountDetailDao bankAccountDetailDao;
	private TallySetDao tallySetDao;
	private QueryDao queryDao;
	private CodeRuleDao codeRuleDao;
	private IdGenerator idGenerator;
	private LoanBillService loanBillService;
	private DataSource dataSource;

	public String getCountView() {
		return this.countView;
	}

	public void setCountView(String countView) {
		this.countView = countView;
	}

	public ModelAndView handleExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String idStr = request.getParameter("id_str");
		String flag = request.getParameter("flag");
		boolean examined = false;
		if (flag != null && flag.equals("1")) {
			examined = true;
		}

		String[] arr = idStr.split(";");
		boolean num = false;
		String msg;
		byte arg16;
		if (!examined) {
			try {
				for (int examinerId = 0; examinerId < arr.length; ++examinerId) {
					this.loanBillService.txUnExamine(request,
							Long.parseLong(arr[examinerId]), this.loanBillDao,
							this.bankAccountBalanceDao,
							this.bankAccountDetailDao, this.idGenerator, ub);
				}

				msg = "反审核成功";
			} catch (CannotAcquireLockException arg15) {
				msg = "事务冲突故障，请重新操作！";
			}

			arg16 = 1;
		} else {
			long arg17 = ub.getStaffer().getId();
			String examinerName = ub.getStaffer().getStafferName();

			try {
				for (int w = 0; w < arr.length; ++w) {
					this.loanBillService.txExamine(request,
							Long.parseLong(arr[w]), this.loanBillDao, arg17,
							examinerName, this.bankAccountBalanceDao,
							this.idGenerator);
				}

				msg = "审核成功";
			} catch (CannotAcquireLockException arg14) {
				msg = "事务冲突故障，请重新操作！";
			}

			arg16 = 1;
		}

		mv.addObject("returnValue", String.valueOf(arg16));
		EncCommonHelper.setAlertMessage(request, msg);
		return mv;
	}

	public ModelAndView handleListForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForExamineView);
		String searchKey = request.getParameter("search_key");
		String type = request.getParameter("type");
		String tsId = request.getParameter("ts_id");
		if (type != null && type.equals("IN")) {
			if (!EncCommonHelper.checkPrivilege(request, "LOAN_IN_EXAM")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (type != null && type.equals("OUT")
				&& !EncCommonHelper.checkPrivilege(request, "LOAN_OUT_EXAM")) {
			throw new PermissionDennyException("Permission Denny");
		}

		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.loanBillDao.getListCount(searchKey, type, false,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.loanBillDao.getList(searchKey, type, false,
				Long.parseLong(tsId), ub, allowStart, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("searchKey", searchKey);
		mv.addObject("type", type);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForExamineView);
		String id = request.getParameter("id");
		this.getLoanBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForUnExamineView);
		String searchKey = request.getParameter("search_key");
		String type = request.getParameter("type");
		String tsId = request.getParameter("ts_id");
		if (type != null && type.equals("IN")) {
			if (!EncCommonHelper.checkPrivilege(request, "LOAN_IN_UN_EXAM")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (type != null && type.equals("OUT")
				&& !EncCommonHelper.checkPrivilege(request, "LOAN_OUT_UN_EXAM")) {
			throw new PermissionDennyException("Permission Denny");
		}

		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.loanBillDao.getListCount(searchKey, type, true,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.loanBillDao.getList(searchKey, type, true,
				Long.parseLong(tsId), ub, allowStart, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("searchKey", searchKey);
		mv.addObject("type", type);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForUnExamineView);
		String id = request.getParameter("id");
		this.getLoanBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForCancelView);
		String searchKey = request.getParameter("search_key");
		String type = request.getParameter("type");
		String tsId = request.getParameter("ts_id");
		if (type != null && type.equals("IN")) {
			if (!EncCommonHelper.checkPrivilege(request, "LOAN_IN_CANC")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (type != null && type.equals("OUT")
				&& !EncCommonHelper.checkPrivilege(request, "LOAN_OUT_CANC")) {
			throw new PermissionDennyException("Permission Denny");
		}

		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.loanBillDao.getListCount(searchKey, type,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.loanBillDao.getList(searchKey, type,
				Long.parseLong(tsId), ub, allowStart, "", startIndex,
				perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("searchKey", searchKey);
		mv.addObject("type", type);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForCancelView);
		String id = request.getParameter("id");
		this.getLoanBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String id = request.getParameter("id");
		String reason = request.getParameter("reason");

		String msg;
		try {
			this.loanBillService.txCancel(request, Long.parseLong(id), reason,
					this.loanBillDao, ub, this.bankAccountBalanceDao,
					this.bankAccountDetailDao, this.idGenerator);
			msg = "作废成功！";
		} catch (CannotAcquireLockException arg8) {
			msg = "事务冲突故障，请重新操作！";
		}

		mv.addObject("returnValue", id);
		EncCommonHelper.setAlertMessage(request, msg);
		return mv;
	}

	private LoanBill getLoanBill(ModelAndView mv, long id) {
		LoanBill loanBill = (LoanBill) this.loanBillDao.get(LoanBill.class, id);
		if (loanBill != null && loanBill.getTallySetId() != 0L) {
			mv.addObject(
					"tallySet",
					this.loanBillDao.get(TallySet.class,
							loanBill.getTallySetId()));
		}

		mv.addObject("record", loanBill);
		if (loanBill.getAttachFile() != null
				&& !loanBill.getAttachFile().equals("")) {
			int beginIndex = loanBill.getAttachFile().lastIndexOf("/");
			mv.addObject("attFile",
					loanBill.getAttachFile().substring(beginIndex + 1));
		}

		mv.addObject("bankAccountList", this.bankAccountDao.getList("", ""));
		mv.addObject("departmentList", this.departmentDao.getList(true));
		return loanBill;
	}

	public ModelAndView handleSearchResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.searchResultView);
		String contentType = request.getParameter("content_type");
		String type = request.getParameter("type");
		if (type != null && type.equals("IN")) {
			if (!EncCommonHelper.checkPrivilege(request, "LOAN_IN_SEAR")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (type != null && type.equals("OUT")
				&& !EncCommonHelper.checkPrivilege(request, "LOAN_OUT_SEAR")) {
			throw new PermissionDennyException("Permission Denny");
		}

		if (contentType != null && contentType.equals("EXCEL")) {
			mv = new ModelAndView(this.downloadView);
		}

		List bankAccountList = this.bankAccountDao.getList("", "");
		List depList = this.departmentDao.getList(true);
		QueryParameterBean b = EncLoanBillMACtrHelper.handleSearchResult(
				request, this.dataSource);
		String[] countObjs = new String[]{"sum(b.cost)"};
		QueryResult qr;
		if (contentType != null && contentType.equals("EXCEL")) {
			qr = this.queryDao.doQuery(b.getQParas(), b.getOParas(),
					(String[]) null, b.getShortNameMap(), b.getRetObjs(),
					countObjs);
			LoanBillListExcelView arg17 = new LoanBillListExcelView();
			HashMap arg19 = new HashMap();
			arg19.put("list", qr.getList());
			arg19.put("baList", bankAccountList);
			arg19.put("depList", depList);
			arg19.put("type", request.getParameter("type"));
			arg19.put("loanTotal", qr.getExtCount()[0]);
			return new ModelAndView(arg17, arg19);
		} else {
			qr = this.queryDao.doQuery(b.getCurrentPage(), b.getPageSize(),
					b.getQParas(), b.getOParas(), (String[]) null,
					b.getShortNameMap(), b.getRetObjs(), countObjs);
			ArrayList arr = new ArrayList();

			for (int ent = 0; ent < qr.getList().size(); ++ent) {
				LoanBillBean ub = new LoanBillBean();
				LoanBill bill = (LoanBill) qr.getList().get(ent);
				ub.setLoanBill(bill);
				String baName = "";
				if (bill.getBankAccountId() != 0L) {
					for (int j = 0; j < bankAccountList.size(); ++j) {
						BankAccount ba = (BankAccount) bankAccountList.get(j);
						if (ba.getId() == bill.getBankAccountId()) {
							baName = ba.getBankName();
							if (ba.getAccountName() != null
									&& !ba.getAccountName().equals("")) {
								baName = baName + "(" + ba.getAccountName()
										+ ")";
							}
						}
					}
				}

				ub.setBankAccountName(baName);
				arr.add(ub);
			}

			qr.setList(arr);
			mv.addAllObjects(b.getParameters());
			mv.addObject("qr", qr);
			mv.addObject("loanTotal", qr.getExtCount()[0]);
			mv.addObject("depList", depList);
			EnterpriseInfo arg18 = EncCommonHelper.getEnterpriseInfo(request);
			UserBean arg20 = EncCommonHelper.getCurrentUserAccount(request);
			mv.addObject("myBaList", PageHelper.parseBaList(bankAccountList,
					arg18, arg20, 0L, 0L));
			mv.addObject("tsList",
					PageHelper.getTallySetList(this.tallySetDao, arg20));
			return mv;
		}
	}

	public ModelAndView handlePrint(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.printView);
		String id = request.getParameter("id");
		LoanBill loanBill = this.getLoanBill(mv, Long.parseLong(id));
		if (loanBill != null) {
			if (loanBill.getBillType().equals("IN")) {
				mv.addObject("codeRule", this.codeRuleDao.get("LOAN_BILL_IN"));
			} else {
				mv.addObject("codeRule", this.codeRuleDao.get("LOAN_BILL_OUT"));
			}
		}

		mv.addObject("retVal", request.getParameter("ret_val"));
		return mv;
	}

	public ModelAndView handleCount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.countView);
		String type = request.getParameter("type");
		String tsId = request.getParameter("ts_id");
		if (type != null && type.equals("IN")) {
			if (!EncCommonHelper.checkPrivilege(request, "LOAN_IN_COUN")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (type != null && type.equals("OUT")
				&& !EncCommonHelper.checkPrivilege(request, "LOAN_OUT_COUN")) {
			throw new PermissionDennyException("Permission Denny");
		}

		String searchType = request.getParameter("search_type");
		String mngName = "";
		String rp = "";
		String startDateStr = "";
		String endDateStr = "";
		Date startDate = null;
		Date endDate = null;
		mv.addObject("mngName", request.getParameter("mng_name"));
		mv.addObject("rp", request.getParameter("rp"));
		mv.addObject("startDate", request.getParameter("start_date"));
		mv.addObject("endDate", request.getParameter("end_date"));
		mv.addObject("tsId", request.getParameter("ts_id"));
		mv.addObject("type", request.getParameter("type"));
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				rp = request.getParameter("rp");
			} else {
				String cList = request.getParameter("cb_rp");
				String remCost = request.getParameter("cb_mng");
				String total = request.getParameter("cb_loan_date");
				if (cList != null && cList.equals("1")) {
					rp = request.getParameter("rp");
					mv.addObject("cbRp", cList);
				}

				if (remCost != null && remCost.equals("1")) {
					mngName = request.getParameter("mng_name");
					mv.addObject("cbMng", remCost);
				}

				if (total != null && total.equals("1")) {
					startDateStr = request.getParameter("start_date");
					endDateStr = request.getParameter("end_date");
					SimpleDateFormat perPageNum = new SimpleDateFormat(
							"yyyy-MM-dd");
					startDate = perPageNum.parse(startDateStr);
					endDate = perPageNum.parse(EncCommonHelper.getNextDateStr(
							endDateStr, "yyyy-MM-dd"));
					mv.addObject("cbLoanDate", total);
				}
			}
		} else {
			searchType = "1";
			tsId = String.valueOf(ub.getUser().getDfTsId());
			mv.addObject("tsId", tsId);
		}

		List cList1 = this.loanBillDao.getCountTotal(type, startDate, endDate,
				rp, mngName, Long.parseLong(tsId));
		BigDecimal remCost1 = new BigDecimal(0);
		int total1 = 0;
		if (cList1.size() > 0 && cList1.get(0) != null) {
			Object[] perPageNum1 = (Object[]) cList1.get(0);
			if (perPageNum1[0] != null) {
				total1 = Integer.parseInt(perPageNum1[0].toString());
				remCost1 = (BigDecimal) perPageNum1[1];
			}
		}

		int perPageNum2 = EncCommonHelper.getPageSize(request);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total1,
				perPageNum2);
		int startIndex = (pageno - 1) * perPageNum2;
		List list = this.loanBillDao.getCountList(type, startDate, endDate, rp,
				mngName, Long.parseLong(tsId), startIndex, perPageNum2);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total1,
				perPageNum2, pageno));
		mv.addObject("remCost", remCost1);
		mv.addObject("tsList", PageHelper.getTallySetList(this.tallySetDao, ub));
		mv.addObject("searchType", searchType);
		return mv;
	}

	public String getDialogSuccessView() {
		return this.dialogSuccessView;
	}

	public void setDialogSuccessView(String dialogSuccessView) {
		this.dialogSuccessView = dialogSuccessView;
	}

	public LoanBillDao getLoanBillDao() {
		return this.loanBillDao;
	}

	public void setLoanBillDao(LoanBillDao loanBillDao) {
		this.loanBillDao = loanBillDao;
	}

	public String getInfoForExamineView() {
		return this.infoForExamineView;
	}

	public void setInfoForExamineView(String infoForExamineView) {
		this.infoForExamineView = infoForExamineView;
	}

	public String getInfoForUnExamineView() {
		return this.infoForUnExamineView;
	}

	public void setInfoForUnExamineView(String infoForUnExamineView) {
		this.infoForUnExamineView = infoForUnExamineView;
	}

	public String getListForExamineView() {
		return this.listForExamineView;
	}

	public void setListForExamineView(String listForExamineView) {
		this.listForExamineView = listForExamineView;
	}

	public String getListForUnExamineView() {
		return this.listForUnExamineView;
	}

	public void setListForUnExamineView(String listForUnExamineView) {
		this.listForUnExamineView = listForUnExamineView;
	}

	public String getSearchResultView() {
		return this.searchResultView;
	}

	public void setSearchResultView(String searchResultView) {
		this.searchResultView = searchResultView;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public String getDownloadView() {
		return this.downloadView;
	}

	public void setDownloadView(String downloadView) {
		this.downloadView = downloadView;
	}

	public String getInfoForCancelView() {
		return this.infoForCancelView;
	}

	public void setInfoForCancelView(String infoForCancelView) {
		this.infoForCancelView = infoForCancelView;
	}

	public String getListForCancelView() {
		return this.listForCancelView;
	}

	public void setListForCancelView(String listForCancelView) {
		this.listForCancelView = listForCancelView;
	}

	public LoanBillService getLoanBillService() {
		return this.loanBillService;
	}

	public void setLoanBillService(LoanBillService loanBillService) {
		this.loanBillService = loanBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public BankAccountDetailDao getBankAccountDetailDao() {
		return this.bankAccountDetailDao;
	}

	public void setBankAccountDetailDao(
			BankAccountDetailDao bankAccountDetailDao) {
		this.bankAccountDetailDao = bankAccountDetailDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}

	public String getPrintView() {
		return this.printView;
	}

	public void setPrintView(String printView) {
		this.printView = printView;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}
}